package com.hubu.file;
import java.io.*;
import java.util.*;
import java.util.UUID;
import java.util.concurrent.*;
public class FileSort {
    private static ThreadPoolExecutor executor=new ThreadPoolExecutor(10, 30, 50, TimeUnit.MINUTES, new LinkedBlockingDeque<>(1000), Executors.defaultThreadFactory(), new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {

            throw new RuntimeException("任务超出");
        }
    });
    static Counter counter=new Counter();
    private static final String WRITE_FILE_PATH_PREFIX="write_file_index_";
    private int fileSize=1024;
    private String splitFilePath="d:\\data2\\data\\";
    public void writeStringToFile(String filePath,long count){
        File file=new File(filePath);
        if(!file.exists()||file.isDirectory()){
            throw new RuntimeException("文件必须存在并且不能是文件夹");
        }
        FileOutputStream  fileOutputStream=null;
        BufferedWriter bufferedWriter=null;

        OutputStreamWriter outputStreamWriter=null;

        try {
            fileOutputStream=new FileOutputStream(file,true);
            outputStreamWriter =new OutputStreamWriter(fileOutputStream);
            bufferedWriter=new BufferedWriter(outputStreamWriter);
            try {
                for(int i=1;i<=count;i++){
                    String value= UUID.randomUUID().toString();
                    bufferedWriter.write(value+"\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void testWriteFile(){
        String filePath="D:\\data2\\data\\java.txt";
        List<String> list=new ArrayList<>();
        for(int i=0;i<100;i++){
            list.add(UUID.randomUUID().toString());
        }

        writeListToFile(filePath,list);

    }
    public static  void writeListToFile(String filePath,List<String> values){
        File file=new File(filePath);
        FileOutputStream  fileOutputStream=null;
        BufferedWriter bufferedWriter=null;

        OutputStreamWriter outputStreamWriter=null;

        try {
            fileOutputStream=new FileOutputStream(file,true);
            outputStreamWriter =new OutputStreamWriter(fileOutputStream);
            bufferedWriter=new BufferedWriter(outputStreamWriter);
            try {
                for(int i=0;i<values.size();i++){

                    bufferedWriter.write(values.get(i)+"\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(bufferedWriter!=null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStreamWriter!=null){
                try {
                    outputStreamWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void writeStringToFile(String filePath,String text){
        File file=new File(filePath);
        FileOutputStream  fileOutputStream=null;
        BufferedWriter bufferedWriter=null;

        OutputStreamWriter outputStreamWriter=null;

        try {
            fileOutputStream=new FileOutputStream(file,true);
            outputStreamWriter =new OutputStreamWriter(fileOutputStream);
            bufferedWriter=new BufferedWriter(outputStreamWriter);
            try {
                bufferedWriter.write(text+"\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(bufferedWriter!=null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStreamWriter!=null){
                try {
                    outputStreamWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void splitFileByCount(String filePath){
        List<String> countList=new ArrayList<>();
        File file=new File(filePath);
        if(!file.exists()||file.isDirectory()){
            throw new RuntimeException("文件必须存在并且不能是文件夹");
        }
        FileInputStream fileInputStream=null;
        BufferedReader bufferedReader=null;
        InputStreamReader inputStreamReader=null;
        try {
            fileInputStream=new FileInputStream(file);
            inputStreamReader =new InputStreamReader(fileInputStream);
            bufferedReader=new BufferedReader(inputStreamReader);
            while(true){
                try {
                    String s = bufferedReader.readLine();
                    if(s!=null){
                        countList.add(s);
                    }
                    if(countList.size()==500000){
                        writeListToFile(splitFilePath+WRITE_FILE_PATH_PREFIX+counter.getAndIncrement()+".txt",countList);
                        countList.clear();
                    }
                    //如果读取完毕 最后也需要拷贝
                    if(s==null){
                        if(countList.size()!=500000){
                            writeListToFile(splitFilePath+WRITE_FILE_PATH_PREFIX+counter.getAndIncrement()+".txt",countList);
                            countList.clear();
                        }
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStreamReader!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileInputStream!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void splitFileByHash(String filePath){
        File file=new File(filePath);
        if(!file.exists()||file.isDirectory()){
            throw new RuntimeException("文件必须存在并且不能是文件夹");
        }
        FileInputStream fileInputStream=null;
        BufferedReader bufferedReader=null;

        InputStreamReader inputStreamReader=null;

        try {
            fileInputStream=new FileInputStream(file);
            inputStreamReader =new InputStreamReader(fileInputStream);
            bufferedReader=new BufferedReader(inputStreamReader);
            while(true){
                try {
                    String s = bufferedReader.readLine();
                    if(s==null){
                        break;
                    }
                    int hashIndex=hashFunction(s,fileSize);
                    writeStringToFile(WRITE_FILE_PATH_PREFIX+hashIndex+".txt",s);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStreamReader!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileInputStream!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public String  maxValue(String filePath){
        List<String> maxList=new ArrayList<>();
        File file=new File(filePath);
        File[] files = file.listFiles();
        String name=null;
        if(files!=null&&files.length>0){
            for(File item:files){
                if(item.isFile()&&(name=item.getName()).endsWith("txt")){
                    executor.submit(()->{
                        System.out.println("线程执行任务");
                        String maxValue = findCurrentFileMaxValue(
                                item);
                        synchronized (maxList){
                            maxList.add(maxValue);
                        }
                        //线程提交任务
                    });
                }
            }
        }
        String maxValue=maxList.get(0);
        for(int i=1;i<maxList.size();i++){
            if(maxValue.compareTo(maxList.get(i))<0){
                maxValue= maxList.get(i);
            }
        }
        return maxValue;
    }
    public static String findCurrentFileMaxValue(File file){
        List<String> values=new ArrayList<>();
        if(!file.exists()||file.isDirectory()){
            throw new RuntimeException("文件必须存在并且不能是文件夹");
        }
        FileInputStream fileInputStream=null;
        BufferedReader bufferedReader=null;

        InputStreamReader inputStreamReader=null;

        try {
            fileInputStream=new FileInputStream(file);
            inputStreamReader =new InputStreamReader(fileInputStream);
            bufferedReader=new BufferedReader(inputStreamReader);
            while(true){
                try {
                    String s = bufferedReader.readLine();
                    if(s==null){
                        break;
                    }
                    values.add(s);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String maxValue=values.get(0);
            for(int i=1;i<values.size();i++) {
                if(maxValue.compareTo(values.get(i))<0) {
                    maxValue=values.get(i);
                }
            }
            return maxValue;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStreamReader!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileInputStream!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    public static void testFindMax(String filePath){
        String currentFileMaxValue = findCurrentFileMaxValue(new File(filePath));
        System.out.println(currentFileMaxValue);
    }
    public static void main(String[] args) {




    }
    public int  hashFunction(String value,int fileSize){
        return value.hashCode()&(fileSize-1);
    }
}
